package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lzx.iteam.adapter.EventImageAdapter;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.LegworkRecordData;
import com.lzx.iteam.widget.gridimg.MyGridView;
import com.lzx.iteam.util.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import static com.baidu.location.b.g.i;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 09月 02日
 * Time: 下午1:47
 *
 * @QQ : 1234567890
 * **********************************
 */
public class LegworkDetailActivity extends MyBaseActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;

    /*@FindViewById(id = R.id.legwork_tv_date)
    private TextView mTvDate;
    @FindViewById(id = R.id.legwork_tv_time)
    private TextView mTvTime;
    @FindViewById(id = R.id.legwork_tv_address)
    private TextView mTvAddress;
    @FindViewById(id = R.id.legwork_tv_remark)
    private TextView mTvRemark;
    @FindViewById(id = R.id.legwork_detail_gv_image)
    private MyGridView mGvImage;*/
    @FindViewById(id = R.id.vp_legwork_detail)
    private ViewPager mVpDetail;
    @FindViewById(id = R.id.tv_legwork_detail_pager)
    private TextView mTvPager;

    private ArrayList<View> mViewList;
//    private LegworkRecordData mRecordData;
    private ArrayList<LegworkRecordData> mAllRecordDatas;
    private int mIndex;

    private String[] mImageThumb= null;
    private ArrayList<String> mImageOri;
    private StringBuilder mImages;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.legwork_sign_detail_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mVpDetail.setOnPageChangeListener(this);
    }

    @Override
    public void initData() {
        mTvTitle.setText("签到详情");
        Intent intent = getIntent();
//        mRecordData = (LegworkRecordData) getIntent().getSerializableExtra("record_data");
        mAllRecordDatas = (ArrayList<LegworkRecordData>) intent.getSerializableExtra("record_datas");
        mIndex = intent.getIntExtra("page_index",0);
        mViewList = new ArrayList<>();
        if (mAllRecordDatas != null && mAllRecordDatas.size() > 0) {
            for (int i = 0; i < mAllRecordDatas.size(); i++) {
                View view = getLayoutInflater().from(this).inflate(R.layout.legwork_detail_vp_item, null);

                TextView mTvDate = (TextView) view.findViewById(R.id.legwork_tv_date);
                TextView mTvTime = (TextView) view.findViewById(R.id.legwork_tv_time);
                TextView mTvAddress = (TextView) view.findViewById(R.id.legwork_tv_address);
                TextView mTvRemark = (TextView) view.findViewById(R.id.legwork_tv_remark);
                MyGridView mGvImage = (MyGridView) view.findViewById(R.id.legwork_detail_gv_image);

                LegworkRecordData mRecordData = mAllRecordDatas.get(i);

                mTvDate.setText(DateUtil.getApprovalTime(Long.parseLong(mRecordData.getTime()) * 1000));
                mTvTime.setText("签到时间:" + DateUtil.getHourAndMin(Long.parseLong(mRecordData.getTime()) * 1000));
                mTvAddress.setText(mRecordData.getAddress());

                if (StringUtil.isEmpty(mRecordData.getRemark())){
                    mTvRemark.setText("无");
                }else{
                    mTvRemark.setText(mRecordData.getRemark());
                }

                if(mRecordData.getAttachment()!=null){
                    try {
                        JSONArray attachments = new JSONArray(mRecordData.getAttachment());
                        if (attachments.length() > 0){
                            mImageThumb = new String[attachments.length()];
                            mImageOri = new ArrayList<String>();
                            mImages = new StringBuilder();
                            for (int j = 0; j < attachments.length(); j++) {
                                JSONObject attachment = attachments.getJSONObject(j);
                                mImageThumb[j] = attachment.getString("thumb");
                                mImageOri.add(attachment.getString("ori"));
                                mImages.append(attachment.getString("thumb")).append(",");

                            }
                            EventImageAdapter mImageAdapter = new EventImageAdapter(mActivity,mImageThumb,mImageOri,mImages.substring(0,mImages.length() - 1));
                            mGvImage.setAdapter(mImageAdapter);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }else{
                    mGvImage.setVisibility(View.GONE);
                }
                mViewList.add(view);
            }
            mVpDetail.setAdapter(pagerAdapter);
            mVpDetail.setCurrentItem(mIndex);
            mTvPager.setText(getString(R.string.viewpager_indicator, mIndex + 1, mAllRecordDatas.size()));

        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_ll_back:
                finish();
            break;
        }
    }

    PagerAdapter pagerAdapter = new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {

            return arg0 == arg1;
        }

        @Override
        public int getCount() {

            return mViewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            container.removeView(mViewList.get(position));

        }

        @Override
        public int getItemPosition(Object object) {

            return super.getItemPosition(object);
        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));
            return mViewList.get(position);
        }

    };

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        mTvPager.setText(getString(R.string.viewpager_indicator, i + 1, mAllRecordDatas.size()));
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
